package com.example.a001.part23;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.a001.R;


public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main12);

        // 设置按钮点击事件
        Button button = findViewById(R.id.button_to_next_activity);
        if (button != null) {
            button.setOnClickListener(v -> {
                Log.d("MainActivity", "Button clicked");
                Intent intent = new Intent(MainActivity.this, NextActivity.class);
                intent.putExtra("key", "Hello from MainActivity");
                startActivityForResult(intent, REQUEST_CODE);
            });
        } else {
            Toast.makeText(this, "Button not found", Toast.LENGTH_SHORT).show();
        }

        // 设置窗口内边距监听器
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) {
            String resultData = data.getStringExtra("resultKey");
            Toast.makeText(this, "Received result: " + resultData, Toast.LENGTH_SHORT).show();
        }
    }
}
